/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.performer;

import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringUtil;
import com.tumblr.backboard.Utils.Property;
import ohos.agp.components.Component;

/**
 * Maps the motion of a Component.
 *
 * @author ericleong
 * @since 2021-05-07
 */
public class MapPerformer extends Performer {
    private static final String TAG = "MapPerformer";
    private float mInitialStart;
    private float mInitialEnd;
    private float mStart;
    private float mEnd;

    /**
     * Constructor. Maps the spring domain [0, 1] to the view property range [0, 1].
     *
     * @param target the view to modify.
     * @param property the view property to modify.
     */
    public MapPerformer(Component target, Property<Component, Float> property) {
        this(target, property, 0, 1, 0, 1);
    }

    /**
     * Constructor. Maps the spring domain [0, 1] to the specified view property range.
     *
     * @param target the view to modify.
     * @param property the view property to modify.
     * @param start the minimum value for the view property range.
     * @param end the maximum value for the view property range.
     */
    public MapPerformer(Component target, Property<Component, Float> property, float start, float end) {
        this(target, property, 0, 1, start, end);
    }

    /**
     * Constructor. Maps the spring domain [0, 1] to the specified view property range.
     *
     * @param target the view to modify.
     * @param property the view property to modify.
     * @param initialStart the minimum value for the spring domain.
     * @param initialEnd the maximum value for the spring domain.
     * @param start the minimum value for the view property range.
     * @param end the maximum value for the view property range.
     */
    public MapPerformer(Component target, Property<Component, Float> property, float initialStart, float initialEnd,
            float start, float end) {
        super(target, property);
        this.mInitialStart = initialStart;
        this.mInitialEnd = initialEnd;
        this.mStart = start;
        this.mEnd = end;
    }

    @Override
    public void onSpringUpdate(Spring spring) {
        mProperty.set(mTarget, (float) SpringUtil.mapValueFromRangeToRange(spring.getCurrentValue(), mInitialStart,
                mInitialEnd, mStart, mEnd));
    }
}
